package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FindCommonEllementsCommand {
	
	public static void main(String[] args) {
		List<Integer> commonIntegers = new FindCommonEllementsCommand().execute(Arrays.asList(1, 2, 3, 4, 5, 6, 13,
				17, 18, 23, 27, 4, 5, 6), Arrays.asList(4, 5, 6, 7, 8, 9, 13,
						19, 23, 25, 27, 13, 23));
		
		for (Integer intInList : commonIntegers) {
			System.out.println(intInList);
		}
		
	}
	
	List<Integer> execute(List<Integer> list1, List<Integer> list2) {
		
		Set<Integer> set1 = new HashSet<>(list1);
		Set<Integer> set2 = new HashSet<>(list2);
		List<Integer> common = new ArrayList<>();

		for (Integer intInSet1 : set1) {
			if (set2.contains(intInSet1)) {
				common.add(intInSet1);
			}
		}

		return common;
	}

}
